<template>
  <div>
    <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px" class="">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in peoples" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
export default {
  props: {
    curId: {
      type: String,
      default: ''
    },
    allList: {
      type: Array,
      default() {
        return []
      }
    },
    isEdit: {
      type: Boolean
    }
  },
  data() {
    // code不能重复
    const validateCode = (rule, value, callback) => {
      let codeList = this.isEdit ? this.allList.filter(i => this.curForm.code !== i.code) : this.allList
      codeList = codeList.map(i => i.code)
      if (codeList.includes(value)) {
        return callback(new Error('当前code码重复'))
      } else {
        callback()
      }
    }
    // name同级不能重复
    const validateName = (rule, value, callback) => {
      // 优雅写法
      let nameList = this.isEdit
        ? this.allList.filter(i => i.pid === this.curForm.pid && this.curForm.name !== i.name)
        : this.allList.filter(i => i.pid === this.curId)
      nameList = nameList.map(i => i.name)
      if (nameList.includes(value)) {
        return callback(new Error('当前name重复'))
      } else {
        callback()
      }
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: validateName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: validateCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      },
      peoples: [],
      curCode: ''
    }
  },
  async created() {
    await this.hGetEmployeeSimple()
    if (this.isEdit) {
      await this.hGetDepartDetail()
    }
  },
  methods: {
    hSubmit() {
      this.$refs['deptForm'].validate(async(valid) => {
        if (!valid) {
          console.log('error submit!!')
          return
        } else {
          this.isEdit ? this.doEdit() : this.doAdd()
          this.$emit('show-no')
          this.$emit('update-depart')
        }
      })
    },
    hCancel() {
      this.$emit('show-no')
    },
    // 获取员工简单列表数据
    async hGetEmployeeSimple() {
      const [, data] = await getEmployeeSimple()
      this.peoples = data
    },
    async hGetDepartDetail() {
      const [, data] = await getDepartDetail(this.curId)
      this.form = data
      // console.log(data)
      // 保存一下当前部门的信息
      this.curForm = {
        code: data.code,
        pid: data.pid,
        name: data.name
      }
    },
    async doAdd() {
      const [err, res] = await addDepartments({ ...this.form, pid: this.curId })
      if (err) {
        this.$message.warning('添加失败')
      } else {
        this.$message.success(`${res.name}添加成功`)
      }
    },
    async doEdit() {
      const [err] = await updateDepartments(this.form)
      if (err) {
        this.$message.warning('更新失败')
      } else {
        this.$message.success(`更新成功`)
      }
    }
  }
}
</script>

<style>

</style>
